package lanqiao;

public class 蛇形矩阵 {
    public static void main(String[] args) {
        int num =process(60,60);
        System.out.println(num);
    }
//    public static int process(int n,int m){
//        int[][] dp = new int[n][n];
//        dp[0][0]=1;
//        System.out.println(dp[0][0]);
//        for (int i = 1; i <n; i++) {
//            if (i%2!=0) {
//                for (int k = 0; k <= i; k++) {
//                    if (k == 0) {
//                        dp[0][i] = dp[0][i-1] + 1;
//                    }else {
//                        dp[k][i-k]=dp[k-1][i-k+1]+1;
//                    }
//                }
//            }else {
//                for (int k = 0; k <= i; k++) {
//                    if (k==0) {
//                        dp[i][k] = dp[i-1][k]+1;
//                    }else {
//                        dp[i-k][k]=dp[i-k+1][k-1]+1;
//                    }
//                }
//            }
//        }
//        return dp[19][19];
//    }
public static int process(int n,int m){
    int[][] arr = new int[n][m];
    int tR = 0;
    int tC = 0;
    int dR = 0;
    int dC = 0;
    int endR = arr.length-1;
    int endC = arr[0].length-1;
    boolean fromUp = true;
    int num =0;
    while (tR != endR+1){
        num = add(arr,tR,tC,dR,dC,fromUp,num);
        tR = tC == endC?tR+1:tR;
        tC = tC == endC?tC:tC+1;
        dC = dR == endR?dC+1:dC;
        dR = dR == endR?dR:dR+1;
        fromUp=!fromUp;
    }
    return arr[19][19];
}
public static int add(int[][] arr,int tR,int tC,int dR,int dC,boolean fromUp,int num){
    if (fromUp){
        while (tR!=dR+1){
            arr[tR++][tC--]=++num;
        }
    }else {
        while (dR!=tR-1){
            arr[dR--][dC++] = ++num;
        }
    }
    return num;
}
}
